package org.maven.dependencybrowser.implementations.zip;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.apache.maven.project.interpolation.ModelInterpolationException;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.maven.dependencybrowser.controller.DependencyResolutionException;
import org.maven.dependencybrowser.implementations.Workspace;
import org.maven.dependencybrowser.model.ArtifactInitialisationException;
import org.maven.dependencybrowser.model.PomFile;
import org.maven.dependencybrowser.model.RootPom;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipFileWorkspace extends Workspace implements Contextualizable
{

    public static final String KEY_ZIPFILE_PATH = "zip.filepath";
    public static final String PREFIX_WORKSPACE = "workspace";

    private ZipFile zipFile;
    private String selectedPom;

    ZipFile getZipFile()
    {
        return this.zipFile;
    }

    public String getPath(String moduleName)
    {
        return PREFIX_WORKSPACE + selectedPom.replaceFirst("/pom.xml$", "/" + moduleName + "/pom.xml");
    }

    public PomFile loadModulePom(String module)
    {
        try
        {
            String zipEntryPath = getPath(module);
            ZipEntry zipEntry = zipFile.getEntry(zipEntryPath);
            BufferedReader zipEntryReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
            return loadPom(zipEntryReader);
        }
        catch (IOException e)
        {
            throw new DependencyResolutionException(e);
        }
    }

    public void reloadPomFile() throws ArtifactInitialisationException
    {
        try
        {
            ZipEntry zipEntry = zipFile.getEntry(PREFIX_WORKSPACE + selectedPom);
            BufferedReader zipEntryReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
            root = new RootPom(loadPom(zipEntryReader), this);
        }
        catch (FileNotFoundException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (IOException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (ModelInterpolationException e)
        {
            throw new DependencyResolutionException(e);
        }
    }

    public void loadRootPom(String selectedPom) throws ArtifactInitialisationException
    {
        this.selectedPom = selectedPom;
        reloadPomFile();
    }

    public boolean canCreateSnapshot()
    {
        return false;
    }

    public void contextualize(Context context) throws ContextException
    {
        String zipFilePath = (String) context.get(KEY_ZIPFILE_PATH);
        if (zipFilePath == null || zipFilePath.trim().length() == 0)
        {
            throw new ContextException("Missing configuration parameter: " + KEY_ZIPFILE_PATH);
        }
        try
        {
            this.zipFile = new ZipFile(zipFilePath);
        }
        catch (IOException e)
        {
            throw new ContextException("Error instantiating zip file: " + e.getMessage());
        }
    }

}
